<template>
  <view class="forgot-password-container">
    <!-- 设计说明 -->
    <view class="design-notes">
      <text class="note-title">设计说明</text>
      <text class="note-item">1. 多邻国风格的找回密码界面</text>
      <text class="note-item">2. 响应式布局适配移动设备</text>
      <text class="note-item">3. 支持邮箱/手机号找回</text>
      <text class="note-item">4. 验证码功能</text>
      <text class="note-item">5. 新密码设置与确认</text>
    </view>
    
    <!-- 找回密码表单 -->
    <view class="forgot-password-form">
      <view class="logo-container">
        <image class="logo" src="/static/logo.png"></image>
      </view>
      <view class="form-title">找回密码</view>
      
      <view class="input-group">
        <!-- 找回方式选择 -->
        <view class="recovery-method">
          <text 
            class="method-option" 
            :class="{active: recoveryMethod === 'email'}"
            @click="setRecoveryMethod('email')"
          >邮箱找回</text>
          <text 
            class="method-option" 
            :class="{active: recoveryMethod === 'phone'}"
            @click="setRecoveryMethod('phone')"
          >手机号找回</text>
        </view>
        
        <!-- 邮箱/手机号输入 -->
        <view class="input-item">
          <text class="input-label">{{ recoveryMethod === 'email' ? '邮箱' : '手机号' }}</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              :type="recoveryMethod === 'email' ? 'email' : 'number'" 
              v-model="contactInfo" 
              :placeholder="recoveryMethod === 'email' ? '请输入邮箱' : '请输入手机号'" 
              @blur="validateContactInfo"
            />
            <text v-if="contactInfo && !errors.contactInfo" class="input-icon check">✓</text>
          </view>
          <text v-if="errors.contactInfo" class="error-tip">{{ errors.contactInfo }}</text>
        </view>
        
        <!-- 验证码 -->
        <view class="input-item">
          <text class="input-label">验证码</text>
          <view class="input-wrapper verification-code-wrapper">
            <input 
              class="input verification-code-input" 
              type="text" 
              v-model="verificationCode" 
              placeholder="请输入验证码" 
              @blur="validateVerificationCode"
            />
            <button 
              class="send-code-btn" 
              :disabled="!isContactInfoValid || codeSending || cooldown > 0"
              @click="sendVerificationCode"
            >
              {{ cooldown > 0 ? `${cooldown}秒后重发` : (codeSending ? '发送中...' : '发送验证码') }}
            </button>
          </view>
          <text v-if="errors.verificationCode" class="error-tip">{{ errors.verificationCode }}</text>
        </view>
        
        <!-- 新密码 -->
        <view class="input-item">
          <text class="input-label">新密码</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              :type="showPassword ? 'text' : 'password'" 
              v-model="newPassword" 
              placeholder="请输入新密码" 
              @blur="validateNewPassword"
              @input="checkPasswordStrength"
            />
            <text 
              class="password-toggle" 
              @click="showPassword = !showPassword"
            >{{ showPassword ? '隐藏' : '显示' }}</text>
          </view>
          <text v-if="errors.newPassword" class="error-tip">{{ errors.newPassword }}</text>
          
          <!-- 密码强度指示器 -->
          <view v-if="newPassword" class="password-strength">
            <text class="strength-label">密码强度：</text>
            <view class="strength-bars">
              <view 
                class="strength-bar" 
                :class="{
                  'weak': passwordStrength >= 1,
                  'medium': passwordStrength >= 2,
                  'strong': passwordStrength >= 3
                }"
              ></view>
              <view 
                class="strength-bar" 
                :class="{
                  'medium': passwordStrength >= 2,
                  'strong': passwordStrength >= 3
                }"
              ></view>
              <view 
                class="strength-bar" 
                :class="{
                  'strong': passwordStrength >= 3
                }"
              ></view>
            </view>
            <text class="strength-text">{{ passwordStrengthText }}</text>
          </view>
        </view>
        
        <!-- 确认新密码 -->
        <view class="input-item">
          <text class="input-label">确认新密码</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              :type="showConfirmPassword ? 'text' : 'password'" 
              v-model="confirmPassword" 
              placeholder="请再次输入新密码" 
              @blur="validateConfirmPassword"
            />
            <text 
              class="password-toggle" 
              @click="showConfirmPassword = !showConfirmPassword"
            >{{ showConfirmPassword ? '隐藏' : '显示' }}</text>
          </view>
          <text v-if="errors.confirmPassword" class="error-tip">{{ errors.confirmPassword }}</text>
        </view>
      </view>
      
      <button 
        class="reset-btn" 
        :disabled="!isFormValid || isResetting" 
        :class="{'loading': isResetting}"
        @click="handleResetPassword"
      >
        {{ isResetting ? '提交中...' : '重置密码' }}
      </button>
      
      <view class="login-link">
        <text>记起密码了？</text>
        <text class="link" @click="goToLogin">返回登录</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      recoveryMethod: 'email', // 'email' 或 'phone'
      contactInfo: '',
      verificationCode: '',
      newPassword: '',
      confirmPassword: '',
      showPassword: false,
      showConfirmPassword: false,
      passwordStrength: 0,
      cooldown: 0,
      codeSending: false,
      isResetting: false,
      errors: {
        contactInfo: '',
        verificationCode: '',
        newPassword: '',
        confirmPassword: ''
      }
    }
  },
  computed: {
    isContactInfoValid() {
      return this.contactInfo && !this.errors.contactInfo
    },
    isFormValid() {
      return this.isContactInfoValid && 
             this.verificationCode && 
             this.newPassword && 
             this.confirmPassword && 
             !this.errors.verificationCode && 
             !this.errors.newPassword && 
             !this.errors.confirmPassword
    },
    passwordStrengthText() {
      if (this.passwordStrength === 0) return ''
      if (this.passwordStrength === 1) return '弱'
      if (this.passwordStrength === 2) return '中'
      return '强'
    }
  },
  methods: {
    setRecoveryMethod(method) {
      this.recoveryMethod = method
      this.contactInfo = ''
      this.errors.contactInfo = ''
    },
    validateContactInfo() {
      if (!this.contactInfo) {
        this.errors.contactInfo = this.recoveryMethod === 'email' ? '请输入邮箱' : '请输入手机号'
        return
      }
      
      if (this.recoveryMethod === 'email') {
        // 邮箱验证
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
        if (!emailRegex.test(this.contactInfo)) {
          this.errors.contactInfo = '请输入有效的邮箱地址'
        } else {
          this.errors.contactInfo = ''
        }
      } else {
        // 手机号验证
        const phoneRegex = /^1[3-9]\d{9}$/
        if (!phoneRegex.test(this.contactInfo)) {
          this.errors.contactInfo = '请输入有效的手机号码'
        } else {
          this.errors.contactInfo = ''
        }
      }
    },
    validateVerificationCode() {
      if (!this.verificationCode) {
        this.errors.verificationCode = '请输入验证码'
      } else if (this.verificationCode.length !== 6) {
        this.errors.verificationCode = '验证码应为6位数字'
      } else {
        this.errors.verificationCode = ''
      }
    },
    validateNewPassword() {
      if (!this.newPassword) {
        this.errors.newPassword = '请输入新密码'
      } else if (this.newPassword.length < 6) {
        this.errors.newPassword = '密码长度不能少于6位'
      } else {
        this.errors.newPassword = ''
      }
      
      // 如果确认密码已填写，则同时验证确认密码
      if (this.confirmPassword) {
        this.validateConfirmPassword()
      }
    },
    validateConfirmPassword() {
      if (!this.confirmPassword) {
        this.errors.confirmPassword = '请确认新密码'
      } else if (this.confirmPassword !== this.newPassword) {
        this.errors.confirmPassword = '两次输入的密码不一致'
      } else {
        this.errors.confirmPassword = ''
      }
    },
    checkPasswordStrength() {
      if (!this.newPassword) {
        this.passwordStrength = 0
        return
      }
      
      let strength = 0
      
      // 长度检查
      if (this.newPassword.length >= 8) strength += 1
      
      // 复杂度检查
      if (/[A-Z]/.test(this.newPassword) && /[a-z]/.test(this.newPassword)) strength += 1
      if (/[0-9]/.test(this.newPassword)) strength += 0.5
      if (/[^A-Za-z0-9]/.test(this.newPassword)) strength += 0.5
      
      this.passwordStrength = Math.min(3, Math.floor(strength))
    },
    sendVerificationCode() {
      this.validateContactInfo()
      
      if (this.isContactInfoValid) {
        this.codeSending = true
        
        // 模拟发送验证码
        setTimeout(() => {
          this.codeSending = false
          uni.showToast({
            title: '验证码已发送',
            icon: 'success'
          })
          
          // 开始倒计时
          this.cooldown = 60
          const timer = setInterval(() => {
            this.cooldown--
            if (this.cooldown <= 0) {
              clearInterval(timer)
            }
          }, 1000)
        }, 1500)
      }
    },
    handleResetPassword() {
      // 验证所有字段
      this.validateContactInfo()
      this.validateVerificationCode()
      this.validateNewPassword()
      this.validateConfirmPassword()
      
      if (this.isFormValid) {
        this.isResetting = true
        
        // 模拟重置密码请求
        setTimeout(() => {
          this.isResetting = false
          uni.showToast({
            title: '密码重置成功',
            icon: 'success'
          })
          
          // 重置成功后跳转到登录页
          setTimeout(() => {
            this.goToLogin()
          }, 1500)
        }, 2000)
      }
    },
    goToLogin() {
      uni.navigateTo({
        url: '/pages/login/login'
      })
    }
  }
}
</script>

<style>
.forgot-password-container {
  display: flex;
  min-height: 100vh;
  background-color: #fff;
  position: relative;
}

/* 设计说明样式 */
.design-notes {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  padding: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
  max-width: 200px;
}

.note-title {
  font-weight: bold;
  font-size: 14px;
  margin-bottom: 5px;
  display: block;
  color: #333;
}

.note-item {
  font-size: 12px;
  color: #666;
  display: block;
  margin-bottom: 3px;
}

/* 找回密码表单样式 */
.forgot-password-form {
  width: 100%;
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo-container {
  background-color: #58CC02;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 30px 0 20px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
}

.logo {
  width: 70px;
  height: 70px;
}

.form-title {
  font-size: 28px;
  font-weight: bold;
  color: #4b4b4b;
  margin-bottom: 30px;
}

.input-group {
  width: 100%;
  margin-bottom: 20px;
}

/* 找回方式选择 */
.recovery-method {
  display: flex;
  margin-bottom: 20px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #e0e0e0;
}

.method-option {
  flex: 1;
  text-align: center;
  padding: 12px 0;
  font-size: 16px;
  background-color: #f5f5f5;
  color: #757575;
  transition: all 0.3s ease;
}

.method-option.active {
  background-color: #58CC02;
  color: white;
  font-weight: bold;
}

.input-item {
  margin-bottom: 20px;
  position: relative;
}

.input-label {
  font-size: 15px;
  color: #4b4b4b;
  margin-bottom: 8px;
  display: block;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  transition: border-color 0.3s;
}

.input-wrapper:focus-within {
  border-color: #58CC02;
}

.input {
  flex: 1;
  height: 48px;
  padding: 0 15px;
  font-size: 16px;
  border: none;
  background: transparent;
}

.input-icon {
  margin-right: 15px;
  font-size: 18px;
}

.input-icon.check {
  color: #58CC02;
}

.password-toggle {
  margin-right: 15px;
  color: #58CC02;
  font-size: 14px;
}

.error-tip {
  color: #ff4d4f;
  font-size: 12px;
  margin-top: 5px;
  display: block;
}

/* 验证码样式 */
.verification-code-wrapper {
  display: flex;
}

.verification-code-input {
  flex: 1;
}

.send-code-btn {
  height: 48px;
  padding: 0 15px;
  background-color: #58CC02;
  color: white;
  border: none;
  border-radius: 0 8px 8px 0;
  font-size: 14px;
  white-space: nowrap;
  box-shadow: 0 4px 0 rgba(88, 204, 2, 0.3);
  transition: all 0.3s;
}

.send-code-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(88, 204, 2, 0.3);
}

.send-code-btn:disabled {
  background-color: #a9a9a9;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.1);
  color: #e0e0e0;
}

/* 密码强度指示器 */
.password-strength {
  margin-top: 10px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.strength-label {
  font-size: 12px;
  color: #757575;
  margin-right: 10px;
}

.strength-bars {
  display: flex;
  margin-right: 10px;
}

.strength-bar {
  width: 20px;
  height: 6px;
  margin-right: 3px;
  background-color: #e0e0e0;
  border-radius: 3px;
}

.strength-bar.weak {
  background-color: #ff4d4f;
}

.strength-bar.medium {
  background-color: #faad14;
}

.strength-bar.strong {
  background-color: #52c41a;
}

.strength-text {
  font-size: 12px;
  color: #757575;
}

/* 重置按钮 */
.reset-btn {
  width: 100%;
  height: 50px;
  background-color: #58CC02;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: bold;
  margin-top: 10px;
  box-shadow: 0 4px 0 rgba(88, 204, 2, 0.3);
  transition: all 0.3s;
}

.reset-btn:active {
  transform: translateY(4px);
  box-shadow: none;
}

.reset-btn:disabled {
  background-color: #a9a9a9;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.1);
}

.reset-btn.loading {
  opacity: 0.8;
}

/* 登录链接 */
.login-link {
  margin-top: 20px;
  font-size: 14px;
  color: #757575;
}

.link {
  color: #58CC02;
  font-weight: bold;
  margin-left: 5px;
}
</style>